package com.ljd.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ljd.gulimall.product.entity.BrandEntity;
import com.ljd.gulimall.product.entity.CategoryEntity;
import com.ljd.gulimall.product.service.BrandService;
import com.ljd.gulimall.product.service.CategoryService;
import com.ljd.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ljd.gulimall.product.entity.CategoryBrandRelationEntity;
import com.ljd.gulimall.product.service.CategoryBrandRelationService;
import com.ljd.common.utils.PageUtils;
import com.ljd.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author ljd
 * @email 2381773911@qq.com
 * @date 2023-02-06 17:20:05
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    CategoryService categoryService;
    @Autowired
    BrandService brandService;

    // brands/list
    /**
     * 列表
     */
    @RequestMapping("/brands/list")
    public R getBrand(@RequestParam Long catId){
       List<BrandVo> page = categoryBrandRelationService.getBrand(catId);

        return R.ok().put("data", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/catelog/list")
    public R list(@RequestParam Long brandId){
       List<CategoryBrandRelationEntity> page = categoryBrandRelationService.queryList(brandId);

        return R.ok().put("data", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        CategoryEntity category = categoryService.getById(catelogId);
        BrandEntity brand = brandService.getById(brandId);

        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
